package com.vf.admin.utils;

import com.vf.admin.platform.entity.Menu;
import com.vf.admin.platform.entity.Organ;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 递归工具类
 */
public class RecuUtils {
    public static RecuUtils getInstance(){
        return new RecuUtils();
    }
    /**
     * 根据组织机构父编号获取所有子机构列表
     */
    public List<Organ> findOranList(List<Organ> orgList, int pid,boolean isTop){
        List<Organ> retOrgList=new ArrayList<Organ>();
        for (int i = 0;i< orgList.size();i++) {
            Organ organ = orgList.get(i);
            if (organ.getPid().intValue() == pid || (isTop && organ.getId().intValue() == pid)) {//取出所有父菜单
                Organ organNew = new Organ();
                organNew.setId(organ.getId());
                organNew.setTitle(organ.getTitle());
                organNew.setPid(organ.getPid());
                organNew.setPname((organ.getPid()!=null && organ.getPid().intValue() == -1 ) ?  "当前是最高等级": organ.getPname());
                organNew.setChildren(findOranList(orgList, organ.getId(),false));
                if(organNew.getChildren() == null || organNew.getChildren().size()<=0){
                    organNew.setChildren(null);
                }
                retOrgList.add(organNew);
            }
        }
        return retOrgList;
    }

    /**
     * 根据组织机构父编号获取所有子机构ID列表
     */
    public List<Integer> findOranIdList(List<Organ> orgList, int pid,boolean isTop){
        List<Integer> retOrgIdList=new ArrayList<Integer>();
        for (int i = 0;i< orgList.size();i++) {
            Organ organ = orgList.get(i);
            if (organ.getPid().intValue() == pid || (isTop && organ.getId().intValue() == pid)) {//取出所有父菜单
                retOrgIdList.add(organ.getId());
                if(organ.getChildren()!=null && organ.getChildren().size()>0){
                    List<Integer> orgIdListBf = findOranIdList(organ.getChildren(), organ.getId(),false);
                    retOrgIdList.addAll(orgIdListBf);
                }
            }
        }
        return retOrgIdList;
    }


    /**
     * 根据菜单的父编号获取所有子菜单列表
     */
    public List<Menu> findMenuList(List<Menu> list, int pid){
        List<Menu> menuList=new ArrayList<Menu>();
        for (int i = 0;i< list.size();i++) {
            Menu menu = list.get(i);
            if (menu.getPid() == pid ) {//取出所有父菜单
                Menu menuNew = new Menu();
                menuNew.setId(menu.getId());
                menuNew.setTitle(menu.getTitle());
                menuNew.setPid(menu.getPid());
                menuNew.setSpread(menu.getSpread());
                menuNew.setChecked(menu.getChecked());
                menuNew.setElementList(menu.getElementList());
                menuNew.setChildren(findMenuList(list, menu.getId()));
                if(menuNew.getChildren() == null || menuNew.getChildren().size()<=0){
                    menuNew.setChildren(null);
                }
                menuList.add(menuNew);
            }
        }
        return menuList;
    }

    /**
     * 根据菜单的父编号获取所有子菜单列表
     */
    public List<Menu> findLeftMenuList(List<Menu> list, int pid){
        List<Menu> menuList=new ArrayList<Menu>();
        for (int i = 0;i< list.size();i++) {
            Menu menu = list.get(i);
            if (menu.getPid() == pid ) {//取出所有父菜单
                Menu menuNew = new Menu();
                menuNew.setIcon(menu.getIcon());
                menuNew.setLinks(menu.getLinks());
                menuNew.setTitle(menu.getTitle());
                menuNew.setSorts(menu.getSorts() == null ? 0 :menu.getSorts());
                menuNew.setList(findLeftMenuList(list, menu.getId()));
                if(menuNew.getList() == null || menuNew.getList().size()<=0){
                    menuNew.setList(null);
                }
                menuList.add(menuNew);
            }
        }
        menuList = menuList.stream().sorted(Comparator.comparing(Menu::getSorts).reversed()).collect(Collectors.toList());
        return menuList;
    }
}
